
<title>计算工资 - 工资结算管理</title>


<div class="row">
	<div class="col-xs-12">

		<div class="widget-box">
			<div class="widget-header">选择计算条件</div>

			<div class="widget-body" style="display: block;">
				<form id="conditionForm" action="" method="post">
					<div class="widget-main">
						<div class="row">
							<div
								class="col-md-4  col-md-push-1 col-lg-4 col-lg-push-1">
								<label>部门</label> <select id="depList" class="form-control"></select>
								<br />
							</div>
															
							<div class="col-md-4  col-md-push-1  col-lg-4 col-lg-push-1">
								<label>发放月</label><select id="monthList" class="form-control"></select>
								<br />
								<!-- 点击 <计算> 按钮动态加载结果; 由后端进行表头设置和数据计算 -->
							</div>
							
							<div class="col-md-3  col-md-push-1 col-lg-3 col-lg-push-1">
								<br />
								<input id="calculate" type="submit" value="计算" class="btn btn-primary"/>
								<input type="reset" value="重选" class="btn btn-primary"/>
							</div>
						</div>
					</div>

				</form>

			</div>
		</div>



		<!-- 系统默认不显示已发放工资的员工工资项，可从工资暂存表 _salary_temp_ 中获取 [isDistributed]=true 的员工集合 -->
		<br />
		<table id="show" class="table table-bordered table-hover text-center"></table>

		<!-- 修改数据库中的错误信息，如 [迟到天数] -->
		<!-- <button>修改</button> -->
		<!-- 将跨表查询因数计算的结果数据存入工资暂存表 _salary_temp_ ; 二次暂存update -->
		<!-- <button>暂存</button> -->

	</div>
</div>

<script type="text/javascript">
	var scripts = [null, null];
	var tdInpSwitch = function() {
		﻿var tbid = "#show";
		var pre = null;
		var tdinputselection="";

		function getSelectionStart(o) {
		    if (o.createTextRange) {
		        var r = document.selection.createRange().duplicate();
		        r.moveEnd('character', o.value.length);
		        if (r.text == '') return o.value.length
		        return o.value.lastIndexOf(r.text);
		    } else return o.selectionStart;
		}
		
		function getSelectedText() {
		    if (window.getSelection) {
		        return window.getSelection().toString();
		    }
		    else if (document.getSelection) {
		        return document.getSelection();
		    }
		    else if (document.selection) {
		        return document.selection.createRange().text;
		    }
		}

		function stopevent(event) {
		    event.stopPropagation();
		}

		function inpleave(event, obj) {
		    stopevent(event);
		    doinpleave(obj);
		}

		function doinpleave(obj) {
		    if (!obj) return;
		    var val = $(obj).val();
		    var $par = $(obj).parent()[0];
		    $($par).text(val);
		    pre = null;
		}

		function switchtdinput(obj) {
		    if (pre) doinpleave(pre);
		    var width = $(obj).width() * 0.8;
		    var objtxt=$.trim($(obj).text());
		    
		    $(obj).html("<input type='text' value='"+ objtxt +"'/>");
		    
		    pre = $(obj).find('input').css("text-align", "center").css("border", "1px solid white").width(width)[0];
		    $(obj).find('input').trigger("focus").trigger("select");
		    $(obj).find('input').click(function(event) {
		        stopevent(event);
		        return false;
		    }).blur(function(event) {
		        inpleave(event, this);
		    }).keypress(function(event) {
		        stopevent(event);
		    }).keyup(function(event) {
		        return setpreornext(event, this);
		    }).keydown(function(event) {
			    tdinputselection=getSelectedText();
		        stopevent(event);
		    });
		}
		
		function setpreornext(event, objThis) {
		    var count = -1;
		    stopevent(event);
		    var obj;
		    if (event.keyCode == 40 || event.which == 40 || event.keyCode == 38 || event.which == 38 || event.keyCode == 13 || event.which == 13|| event.keyCode == 37 || event.which == 37|| event.keyCode == 39 || event.which == 39) {
		       count = parseInt($(objThis).parent().data("i"));
		        if (event.keyCode == 40 || event.which == 40) {
		            obj = $(objThis).parent().parent().next("tr").find("td").get(count);
		        } else if (event.keyCode == 38 || event.which == 38) {
		            obj = $(objThis).parent().parent().prev("tr").find("td").get(count);
		        } else if (event.keyCode == 13 || event.which == 13) {
		            obj = $(objThis).parent().parent().find("td").get(count + 1);
		        }
		        else if (event.keyCode == 37 || event.which == 37) {
		            if(getSelectionStart(objThis)==0&&(tdinputselection!=$(objThis).val())){
		                obj = $(objThis).parent().parent().find("td").get(count - 1);
		            }
		        }
		        else if (event.keyCode == 39 || event.which == 39) {
		            if(getSelectionStart(objThis)==$(objThis).val().length&&(tdinputselection!=$(objThis).val()))
		            obj = $(objThis).parent().parent().find("td").get(count + 1);
		        }
		        if (obj) {
		            doinpleave(objThis);
		            switchtdinput(obj);
		        }
		    }
		    if (event.keyCode == 13 || event.which == 13) return false;
		    else return true;
		}
		

	    $(tbid).click(function(e) {
	        stopevent(e);
	        var cur = e.target;
	        if ($(cur).is("td")) {
	            switchtdinput(cur);
	        }
	    });
	    
	    $(tbid).find("tr").each(function() {
	        $(this).find("td").each(function(i) {
	            $(this).data("i", i);
	        });
	    });
		
	}
	
	$('.page-content-area').ace_ajax('loadScripts', scripts, function() {

		/* 动态加载[部门]列表 */
			$.ajax({
						type : "GET",
						url : "json/department_info.json",
						success : function(data) {
							var options = data;
							$("#depList").empty();
							$("#depList").append("<option value=\"null\">请选择</option>");
							for(var i=0;i<options.length;i++){
								var option = options[i];
								$("#depList").append("<option value=\"" + 
										option.dep_id + "\">" + 
										option.dep_name + "</option>");
							}
						}
			});

			/* 动态加载[发放月]列表 */
			$("#monthList").append("<option value=\"null\">请选择</option>");
			for (var i = 1; i < 13; i++) {
				$("#monthList").append(
						"<option value=\"" + i + "\">" + i + "月</option>");
			}

			/* 查询参数为空的表单验证 和 异步加载结果 */
			$("#conditionForm").bind("submit", function() {
				var err = "";
				if ($("#depList").val() == "null") {
					err += "-- 亲，请指定 [部门] 信息 -- \n";
				}
				if ($("#monthList").val() == "null") {
					err += "- 亲，请指定 [发放月] 信息 - \n";
				}
				if (err == "") {
					/* 异步加载查询结果 */
					$.ajax({
						type : "GET",
						url : "json/salary_cal_vo.json",
						success : function(data) {
							var trs = data;
							$("#show").empty();
							for(var i=0;i<trs.length;i++){
								var salary = trs[i];
								/* 生成表头 */
								if (i == 0) {
									$("#show").append("<thead>"+
											"<tr><th>" + 
										salary.emp_id + "</th><th>" + 
										salary.emp_name + "</th><th>" + 
										salary.final_salary + "</th><th>" + 
										salary.base_salary + "</th><th>" + 
										salary.warm_allowance + "</th><th>" + 
										salary.sickleave_days + "</th><th>" + 
										salary.affairleave_days + "</th><th>" + 
										salary.overwork_days + "</th><th>" + 
										salary.late_days + "</th><th>" + 
										salary.sickleave_hold + "</th><th>" + 
										salary.affairleave_hold + "</th><th>" + 
										salary.overwork_salary + "</th><th>" + 
										salary.ind_pension + "</th><th>" + 
										salary.ind_medical + "</th><th>" + 
										salary.ind_housing_fund + "</th><th>" + 
										salary.com_pension + "</th><th>" + 
										salary.com_medical + "</th><th>" + 
										salary.com_housing_fund + "</th><th>" + 
										salary.ind_tax + "</th><th>" + 
										salary.reissue + "</th>" + 
										"<th>操作</th></tr>" +"</thead><tbody>"
									);
								}
								/* 生成数据 */
								else {
									$("#show").append("<tr id='chgBtn" + i + "Edit'><td>" + 
										salary.emp_id + "</td><td>" + 
										salary.emp_name + "</td><td>" + 
										salary.final_salary + "</td><td>" + 
										salary.base_salary + "</td><td>" + 
										salary.warm_allowance + "</td><td>" + 
										salary.sickleave_days + "</td><td>" + 
										salary.affairleave_days + "</td><td>" + 
										salary.overwork_days + "</td><td>" + 
										salary.late_days + "</td><td>" + 
										salary.sickleave_hold + "</td><td>" + 
										salary.affairleave_hold + "</td><td>" + 
										salary.overwork_salary + "</td><td>" + 
										salary.ind_pension + "</td><td>" + 
										salary.ind_medical + "</td><td>" + 
										salary.ind_housing_fund + "</td><td>" + 
										salary.com_pension + "</td><td>" + 
										salary.com_medical + "</td><td>" + 
										salary.com_housing_fund + "</td><td>" + 
										salary.ind_tax + "</td><td>" + 
										salary.reissue + "</td>" + 
										"<td id='ex'><input type='button' value='暂存' class='btn btn-info btn-xs' /></td></tr>"
									);
								}
							}
							$("#show").append("</tbody>");
							tdInpSwitch();
						}
					});
					return false;
				} else {
					alert(err);
					return false;
				}
			});

	});
</script>
